<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="/Public/js/three.min.js"></script>
    <script src="/Public/js/OrbitControls.js"></script>
    <script src="/Public/js/jquery-3.7.1.min.js"></script>
    <script src="/Public/js/geotiff.js"></script>
    <script src="/Public/js/f3d.js"></script>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background: #1a1a2e;
            font-family: Arial, sans-serif;
        }
    </style>

</head>

<body>
    <script>
        let terrainMesh = null;
        let heightScale = 0.5;
        let smoothness = 3;

        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0x1a1a2e);

        // 创建相机
        const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 10000);
        camera.position.set(1, 10, 3);

        // 创建渲染器
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        // 添加光源
        const ambientLight = new THREE.AmbientLight(0x404040);
        scene.add(ambientLight);
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
        directionalLight.position.set(1, 1, 1);
        scene.add(directionalLight);
        // 添加控制器
        const controls = new THREE.OrbitControls(camera, renderer.domElement);
        controls.enableDamping = true;

        //-------------------------------------

        const plane_geometry = new THREE.PlaneGeometry(5, 5)
        const plane_material = new THREE.MeshBasicMaterial({
            color: '#3f7b9d',
            side: THREE.DoubleSide,
        });
        const plane1 = new THREE.Mesh(plane_geometry, plane_material)
        plane1.rotateX(-Math.PI / 2)
        scene.add(plane1)

        const geometry = new THREE.CircleGeometry(2.0, 8);
        const material = new THREE.MeshBasicMaterial({
            color: 0x00ff00,
            wireframe: true
        });
        const circle = new THREE.Mesh(geometry, material);
        circle.rotateX(-Math.PI / 2)
        circle.updateWorldMatrix(true, false);
        scene.add(circle)

        const line_points = [new THREE.Vector3(1.3, 0, -1.8), new THREE.Vector3(0.2, 0, 2.2)]
        const line_geometry = new THREE.BufferGeometry().setFromPoints(line_points);
        const line_material = new THREE.LineBasicMaterial({
            color: '#ff0000'
        })
        const line = new THREE.Line(line_geometry, line_material)
        scene.add(line)
        // 创建顶点标记
        const positionAttribute = circle.geometry.attributes.position;
        const vector = new THREE.Vector3();
        for (let i = 0; i < positionAttribute.count; i++) {
            vector.fromBufferAttribute(positionAttribute, i)
            circle.localToWorld(vector)
            const markerGeometry = new THREE.SphereGeometry(0.05, 8, 8);
            const markerMaterial = new THREE.MeshBasicMaterial({ color: '#ff0000' });
            const marker = new THREE.Mesh(markerGeometry, markerMaterial);
            marker.position.copy(vector.clone())
            scene.add(marker);
        }

        const cross_point_list = []
        //圆心点与折点组成线
        for (let i = 1; i < positionAttribute.count; i++) {
            const sp = new THREE.Vector3()
            sp.fromBufferAttribute(positionAttribute, 0)
            circle.localToWorld(sp)
            // sp.setY(0.2)

            const ep = new THREE.Vector3()
            ep.fromBufferAttribute(positionAttribute, i)
            circle.localToWorld(ep)
            // ep.setY(0.2)

            const seg_sp = [sp.x, sp.z]
            const seg_ep = [ep.x, ep.z]
            const seg_line = [seg_sp, seg_ep]

            const line_sp = [line_points[0].x, line_points[0].z]
            const line_ep = [line_points[1].x, line_points[1].z]
            const line_ins = [line_sp, line_ep]
            const ins_point = lineSegmentsIntersect(seg_line, line_ins)

            if (ins_point.intersects) {
                cross_point_list.push(ins_point.intersectionPoint)
            }

        }

        //折点首尾级成线
        for (let i = 1; i < positionAttribute.count - 1; i++) {
            const sp = new THREE.Vector3()
            sp.fromBufferAttribute(positionAttribute, i)
            circle.localToWorld(sp)

            const ep = new THREE.Vector3()
            ep.fromBufferAttribute(positionAttribute, i + 1)
            circle.localToWorld(ep)

            const seg_sp = [sp.x, sp.z]
            const seg_ep = [ep.x, ep.z]
            const seg_line = [seg_sp, seg_ep]

            const line_sp = [line_points[0].x, line_points[0].z]
            const line_ep = [line_points[1].x, line_points[1].z]
            const line_ins = [line_sp, line_ep]
            const ins_point = lineSegmentsIntersect(seg_line, line_ins)

            if (ins_point.intersects) {
                cross_point_list.push(ins_point.intersectionPoint)
            }
        }

        for (let i = 0; i < cross_point_list.length; i++) {
            console.log('a')
            const markerGeometry = new THREE.SphereGeometry(0.05, 8, 8);
            const markerMaterial = new THREE.MeshBasicMaterial({ color: '#ffffff' });
            const marker = new THREE.Mesh(markerGeometry, markerMaterial);
            marker.position.set(cross_point_list[i][0], 0.2, cross_point_list[i][1])
            scene.add(marker);
        }



        //-------------------------------------


        // 动画循环
        function animate() {
            requestAnimationFrame(animate);
            controls.update();
            renderer.render(scene, camera);
        }
        animate();


    </script>
</body>

</html>